import {
  checkLogin
} from './auth.js'
new Vue({
  el: '#app',
  data: {
    IS_LOGIN: null,
    searchKey: '', // 搜索的关键词
    userAvatar: ''
  },
  async created() {
    this.IS_LOGIN = await checkLogin()
    if (this.IS_LOGIN == null) {
      location.href = '/'
    }
    this.userAvatar = this.IS_LOGIN.avatar
  },
  methods: {
    // 上传文件
    async uploadFile() {
      if (this.$refs.file.files.length == 0) {
        this.$message.error('请选择文件')
        return 
      }
      const file = this.$refs.file.files[0]
      const formData = new FormData()
      formData.append('file', file)
      const res = await request.post('/admin/file/upload',formData,{
        headers: {
          'Content-Type': 'multipart/form-data'
        }
      })
      const url = res.data
      this.userAvatar = url
      this.IS_LOGIN.avatar = url
    },
    // 查找商品
    searchProduct() {
      console.log('查找商品:');
      location.href = `../web/search.html?searchKey=${this.searchKey}`
    },
    // 退出登录
    logout() {
      localStorage.removeItem('token')
      this.IS_LOGIN = null
      location.href = '/'
    },
  }
})